package com.colorful.insuranceproject.commonActivity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.utils.ProgressWebView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by handsome-Bob on 2017/8/16.
 * <p>
 * 加载网页展示一个不是特定的页面
 */
public class WebViewActivity extends TitleActivity {

    public static final String EXTRA_LOAD_URL = "extra_load_url";

    private String extraUrl;

    @Bind(R.id.wv_activity_web_web)
    ProgressWebView wvWebView;

    @Override
    protected int getContentView() {
        return R.layout.activity_webview;
    }

    @Override
    protected void onInitExtras(@NonNull Bundle bundle) {
        super.onInitExtras(bundle);
        extraUrl = bundle.getString(EXTRA_LOAD_URL);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void onViewInitialized() {
        WebSettings webSettings = wvWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        wvWebView.setWebChromeClient(new ProgressWebView.WebChromeClient(wvWebView) {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                setTitle(title);
            }
        });

        wvWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
//                view.loadUrl(url);
                return false;
            }
        });
        wvWebView.loadUrl(extraUrl);
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }
    @Override
    public void onBackPressed() {
        if (wvWebView != null && wvWebView.canGoBack()) {
            wvWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}
